package com.woniuxy.core.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import java.security.Key;
import java.util.Date;
import java.util.Map;

public class MyJwtUtil {
    //自定义的秘钥
    //HS256：即字节长度>=32
    static String userSect ="ksaasfdasdfadsfwfrasdfsadffcasijfasadfasfdlkfjlasjflksajfd";
    static String adminSect ="ksaasfdasdfadsFWFRAdsfsadffcasijfasadfasfdlkfjlasjflksajfd";
    static String venueSect ="ksaasfdasdfadsVeNuedsfsadffcasijfasadfasfdlkfjlasjflksajfd";
    static String coachSect ="ksaasfdasdfadsCOAChdsfsadffcasijfasadfasfdlkfjlasjflksajfd";
    //生成token
    //键值对包含用户信息
    //以分钟为单位的时间 代表jwt的有效时间
    public static String createUserToken(Map<String, Object> body, Integer mil) {
        Key key = Keys.hmacShaKeyFor(userSect.getBytes());
        return Jwts.builder()
                .setClaims(body)
                .setExpiration(new Date(System.currentTimeMillis() + mil * 60 * 1000))
                .signWith(key)
                .compact();
    }
    public static String createAdminToken(Map<String, Object> body, Integer mil) {
        Key key = Keys.hmacShaKeyFor(adminSect.getBytes());
        return Jwts.builder()
                .setClaims(body)
                .setExpiration(new Date(System.currentTimeMillis() + mil * 60 * 1000))
                .signWith(key)
                .compact();
    }
    public static String createVenueToken(Map<String, Object> body, Integer mil) {
        Key key = Keys.hmacShaKeyFor(venueSect.getBytes());
        return Jwts.builder()
                .setClaims(body)
                .setExpiration(new Date(System.currentTimeMillis() + mil * 60 * 1000))
                .signWith(key)
                .compact();
    }
    public static String createCoachToken(Map<String, Object> body, Integer mil) {
        Key key = Keys.hmacShaKeyFor(coachSect.getBytes());
        return Jwts.builder()
                .setClaims(body)
                .setExpiration(new Date(System.currentTimeMillis() + mil * 60 * 1000))
                .signWith(key)
                .compact();
    }

    //解析token
    public static Claims parseUserToken(String token) {
        Key key = Keys.hmacShaKeyFor(userSect.getBytes());
        return Jwts.parserBuilder().setSigningKey(key)
                .build().parseClaimsJws(token).getBody();
    }
    public static Claims parseVenueToken(String token) {
        Key key = Keys.hmacShaKeyFor(venueSect.getBytes());
        return Jwts.parserBuilder().setSigningKey(key)
                .build().parseClaimsJws(token).getBody();
    }
    public static Claims parseCoachToken(String token) {
        Key key = Keys.hmacShaKeyFor(coachSect.getBytes());
        return Jwts.parserBuilder().setSigningKey(key)
                .build().parseClaimsJws(token).getBody();
    }
    public static Claims parseAdminToken(String token) {
        Key key = Keys.hmacShaKeyFor(adminSect.getBytes());
        return Jwts.parserBuilder().setSigningKey(key)
                .build().parseClaimsJws(token).getBody();
    }
    public static Integer getUserIdByKey(String token,String key){

        return ((Integer)MyJwtUtil.parseUserToken(token).get(key));
    }
    public static Integer getVenueIdByKey(String token,String key){

        return ((Integer)MyJwtUtil.parseVenueToken(token).get(key));
    }
    public static Integer getCoachIdByKey(String token,String key){

        return ((Integer)MyJwtUtil.parseCoachToken(token).get(key));
    }
    public static Integer getAdminIdByKey(String token,String key){

        return ((Integer)MyJwtUtil.parseAdminToken(token).get(key));
    }
}
